package com.tengits.upgrade;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

/**
 * 通用选择对话框(确定和取消)
 *
 * @author
 */
public class UIDialog {
    /**
     * @param context
     * @param 抬头
     * @param 内容
     * @param 确定
     * @param 取消
     * @param 确定回调
     * @param 取消回调
     */
    public static void showUpdateDialog(Activity activity, int isForce, String title, String version, String message, String yes, String no,
                                        final PositiveButtonClickListener positiveButtonClickListener,
                                        final NegativeButtonClickListener negativeButtonClickListener) {

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        final Dialog dialog = builder.create();
        if (activity != null) {
            dialog.show();
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(false);
            View view = LayoutInflater.from(activity).inflate(R.layout.dialog_update, null);
            TextView titleTV = (TextView) view.findViewById(R.id.dialog_title);
            TextView contentTV = (TextView) view.findViewById(R.id.dialog_content);
            TextView versionTV = (TextView) view.findViewById(R.id.dialog_version);
            Button confirmBtn = (Button) view.findViewById(R.id.dialog_btn_confirm);
            Button cancelBtn = (Button) view.findViewById(R.id.dialog_btn_cancel);
            titleTV.setText(title);
            versionTV.setText("V" + version);
            contentTV.setText(message);
            confirmBtn.setText(yes);
            cancelBtn.setText(no);
            if (isForce == 1) {
                cancelBtn.setVisibility(View.GONE);
                dialog.setCancelable(false);
            } else {
                cancelBtn.setVisibility(View.VISIBLE);
            }
            confirmBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (positiveButtonClickListener != null) {
                        positiveButtonClickListener.setPositiveButton(dialog, 0);
                    }
                }
            });

            cancelBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    if (negativeButtonClickListener != null) {
                        negativeButtonClickListener.setNegativeButton(dialog);
                    }
                }
            });

            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = (int) (ScreenUtils.getScreenWidth(activity) * 0.9); // 宽度
            dialogWindow.setAttributes(lp);
            dialog.show();
            dialog.setContentView(view);
        }
    }

    public interface PositiveButtonClickListener {
        void setPositiveButton(DialogInterface dialog, int which);
    }

    public interface NegativeButtonClickListener {
        void setNegativeButton(DialogInterface dialog);
    }
}
